package cn.xinfei.xdecision.pulsar;

import lombok.extern.slf4j.Slf4j;
import org.apache.pulsar.client.api.*;

import java.util.concurrent.TimeUnit;
@Slf4j
public class FirstConsumerDemo {
    public static void main(String[] args) throws PulsarClientException {
        PulsarClient client = PulsarClient.builder()
                .serviceUrl("pulsar://192.168.9.181:6650")
                .build();

        Consumer<String> consumer = client.newConsumer(Schema.STRING)
                .topic("shiyusen-demo-1")
                .subscriptionName("my-subscription-2")
                .ackTimeout(10, TimeUnit.SECONDS)
                .subscriptionType(SubscriptionType.Shared)
                .subscribe();
        // 死循环接收
        while (true){
            Message<String> message = consumer.receive();
            String msgContent = message.getValue();
            System.out.println("接收到消息: {}"+msgContent);
            consumer.acknowledge(message);
        }
    }
}
